package com.sais.manage.boot.daily.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sais.common.utils.UUIDUtils;
import com.sais.manage.boot.building.dto.BuildingDTO;
import com.sais.manage.boot.building.mapper.BuildingMapper;
import com.sais.manage.boot.daily.dto.BuildingChartDTO;
import com.sais.manage.boot.daily.dto.SanitationDTO;
import com.sais.manage.boot.daily.dto.SanitationSaveDTO;
import com.sais.manage.boot.daily.dto.SanitationSearchDTO;
import com.sais.manage.boot.daily.entity.Sanitation;
import com.sais.manage.boot.daily.mapper.SanitationMapper;
import com.sais.manage.boot.daily.service.ISanitationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 宿舍管理系统-卫生登记 service 实现类
 *
 * @author zwp
 * @date 2020/2/2
 **/
@Service
@Slf4j
public class SanitationServiceImpl implements ISanitationService {

    @Resource
    private SanitationMapper sanitationMapper;

    @Resource
    private BuildingMapper buildingMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveSanitation(SanitationSaveDTO sanitationSaveDTO) {
        String ukId = sanitationSaveDTO.getUkId();
        if (StringUtils.isEmpty(ukId)) {
            createSanitation(sanitationSaveDTO);
        } else {
            updateSanitation(sanitationSaveDTO);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean createSanitation(SanitationSaveDTO sanitationSaveDTO) {
        Sanitation sanitation = sanitationSaveDTO.convert();
        sanitation.setUkId(UUIDUtils.getLower());
        sanitationMapper.createSanitation(sanitation);
        return true;
    }


    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateSanitation(SanitationSaveDTO sanitationSaveDTO) {
        Sanitation sanitation = sanitationSaveDTO.convert();
        sanitation.setGmtModified(LocalDateTime.now());
        sanitationMapper.updateById(sanitationSaveDTO.convert());
        return true;

    }


    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteSanitation(String sanitationSaveDTO) {
        sanitationMapper.deleteSanitation(sanitationSaveDTO);
        return true;
    }


    @Override
    public SanitationDTO selectSanitation(String sanitationId) {
        SanitationDTO sanitationDTO = sanitationMapper.selectSanitation(sanitationId);
        return sanitationDTO;
    }


    @Override
    public Page<SanitationDTO> listSanitation(Integer current, Integer size, SanitationSearchDTO sanitationSearchDTO) {
        Page<SanitationDTO> page = new Page<>(current, size);
        return sanitationMapper.listSanitation(page, sanitationSearchDTO);
    }

    @Override
    public Page<SanitationDTO> listSanitationByManage(Integer current, Integer size, SanitationSearchDTO searchDTO, String userId) {
        Page<SanitationDTO> page = new Page<>(current, size);
        Page<SanitationDTO> sanitations = sanitationMapper.listSanitationByManage(page, searchDTO, userId);
        return sanitations;
    }

    @Override
    public List<Integer> listScoreByManage(String userId) {
        List<Integer> scores = sanitationMapper.listScoreByManage(userId);
        return scores;
    }

    @Override
    public List<BuildingChartDTO> listCountBeyondScoreByBuilding() {
        List<BuildingChartDTO> buildingCharts = sanitationMapper.listBuildingScore();
        return buildingCharts;
    }

    @Override
    public Map<String, Integer> CountColumn(SanitationSearchDTO searchDTO) {
        if(StringUtils.isEmpty(searchDTO.getRoomId())){
            Map<String, Integer> countColumn = sanitationMapper.countColumn(searchDTO, null);
            return countColumn;
        }else {
            BuildingDTO buildingDTO = buildingMapper.selectBuildingByRoomId(searchDTO.getRoomId());
            Map<String, Integer> countColumn = sanitationMapper.countColumn(searchDTO, buildingDTO.getUkId());
            return countColumn;
        }
    }

    @Override
    public List<Map> buildCountColumn(SanitationSearchDTO searchDTO) {
        List<Map> list = sanitationMapper.buildCountColumn(searchDTO);
        return list;
    }
}
